/*
Naijanimi Free SMS Application for Nokia mobiles.

"Naijanimi Free SMS Application" is protected by the copyright law
(c) 2011 Enrico Miglino / Tech Consulting (enrico.miglino@ovi.com)
Distributed under the LGPL 3.0 license.

This file is part of "Naijanimi Free SMS Application".

    "Naijanimi Free SMS Application" is free software: you can redistribute it
    and/or modify it under the terms of the Lesser GNU General Public License
    as published by the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    "Naijanimi Free SMS Application" is distributed in the hope that it
    will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    Lesser GNU General Public License for more details.

    You should have received a copy of the Lesser GNU General Public License
    along with "Naijanimi Free SMS Application".
    If not, see <http://www.gnu.org/licenses/>.
*/

import QtQuick 1.0

// Definition of the generic text input miltiline area, used in input forms.
FocusScope {
    id: focusScope

    // Alias to externally manage the input values
    property alias textField: textInput.text        // Input value

    // Values of this item are defined when it is instantiated
    width: 250; height: 150
    property int borderSize: 4
    property int fontPoints: 12

    // Text area background border
    BorderImage {
        source: "images/lineedit-bg.png"
        width: parent.width; height: parent.height
        border {
            left: focusScope.borderSize; top: focusScope.borderSize;
            right: focusScope.borderSize; bottom: focusScope.borderSize
        }
    }

    // Text area backgrond when focused
    BorderImage {
        source: "images/lineedit-bg-focus.png"
        width: parent.width; height: parent.height
        border {
            left: focusScope.borderSize; top: focusScope.borderSize;
            right: focusScope.borderSize; bottom: focusScope.borderSize
        }
        visible: parent.activeFocus ? true : false
    }

    // Dummy text when field is empty or cleared
    Text {
        id: typeSomething

        anchors.fill: parent; anchors.leftMargin: focusScope.borderSize * 2
        verticalAlignment: Text.AlignVCenter
        text: "..."
        color: "gray"
        font.italic: true
        font.pointSize: focusScope.fontPoints
    }

    // Manage focus acquisition when user select field with mouse.
    // When focus is acquired, the virtual keyboard is shown to manage characters input
    MouseArea {
        anchors.fill: parent
        onClicked: {
            focusScope.focus = true;
            textInput.openSoftwareInputPanel();
        }
    }

    // Text area field.
    Flickable {
         id: flick

         width: focusScope.width; height: focusScope.height;
         contentWidth: textInput.paintedWidth
         contentHeight: textInput.paintedHeight
         clip: true

         function ensureVisible(r)
         {
             if (contentX >= r.x)
                 contentX = r.x;
             else if (contentX+width <= r.x+r.width)
                 contentX = r.x+r.width-width;
             if (contentY >= r.y)
                 contentY = r.y;
             else if (contentY+height <= r.y+r.height)
                 contentY = r.y+r.height-height;
         }

         TextEdit {
             id: textInput
             width: flick.width
             height: flick.height
             focus: true
             wrapMode: TextEdit.Wrap
             onCursorRectangleChanged: flick.ensureVisible(cursorRectangle)
         }
     }

    // Clear icon image to the right of text area.
    // Mouse click on this area clears the text field and
    // opes the virtual keyboard
    Image {
        id: clear
        anchors {
            right: parent.right;
            rightMargin: focusScope.borderSize * 2
            verticalCenter: parent.verticalCenter
        }
        source: "images/clear.png"
        opacity: 0

        MouseArea {
            anchors.fill: parent
            onClicked: {
                textInput.text = ''; focusScope.focus = true;
                textInput.openSoftwareInputPanel();
            }
        }
    }

    // State changes management
    states: State {
        name: "hasText"; when: textInput.text != ''
        PropertyChanges { target: typeSomething; opacity: 0 }
        PropertyChanges { target: clear; opacity: 1 }
    }

    // Manage state changes transitions
    transitions: [
        Transition {
            from: ""; to: "hasText"
            NumberAnimation { exclude: typeSomething; properties: "opacity" }
        },
        Transition {
            from: "hasText"; to: ""
            NumberAnimation { properties: "opacity" }
        }
    ]
}
